home *** CD-ROM | disk | FTP | other *** search
- Path: res.com!usenet
- From: danlynes@res.com
- Newsgroups: comp.lang.c
- Subject: Re: New C Programmer Has A Problem
- Date: 30 Jan 1996 02:50:50 GMT
- Organization: RES Online
- Message-ID: <4ek12b$1li@clare.res.com>
- References: <4ehpa3$6kl@nntp.novia.net>
- Reply-To: danlynes@res.com
- NNTP-Posting-Host: di017.res.com
- X-Newsreader: IBM NewsReader/2 v1.2.5
-
- In <4ehpa3$6kl@nntp.novia.net>, tsyslo@oasis.novia.net (Tony Syslo) writes:
-
- > I have a question on C... why isn't this program working?!
- >
- > Program START:
- > /* File Open */
- >
- > #include <clib/dos_protos.h>
-
- First off, this is not ANSI C...if you want an intelligent reply, try recoding
- it using ANSI C, or at least K&R C.
-
- > #include <dos/dos.h>
- > #include <stdio.h>
- > #include <exec/types.h>
-
- The above is too wierd. :)
-
- > void main();
- >
- > char *name;
- > int age;
- >
- > void main()
- >
- > {
- > struct FileHandle *file_handle;
-
- Change above to:
-
- FILE *fpFile ;
-
- > long bytes_written;
- > long bytes_read;
-
- int BytesWritten ;
- int BytesRead ;
-
- > printf("Enter your name: ");
- > scanf("%s",name);
-
- How can you read data into an uninitialized pointer? You need to either
- dynamically allocate storage for the variable, or malloc it.
-
- i.e. After you declare your variables, try doing the following:
-
- name = ( unsigned char * )malloc( 80 ) ;
-
- This will allocate 80 bytes for the string (79 plus a null character).
-
- > printf("\nEnter your age: "); scanf("%d",age);
-
- Keep it one function per line. i.e.:
-
- printf( :\nEnter your age: ") ;
- scanf( "%d", age ) ;
-
- > file_handle=(struct FileHandle *)
- > Open("RAM:You.dat",MODE_NEWFILE);
-
- fpFile = fopen( "RAM:You.Dat", "wb" ) ;
-
- > /* Have we opened the file successfully? */
- > if(file_handle==NULL)
-
- if( !( fpFile ) )
-
- > {
- > printf("Could not open the file!\n");
- > Exit(0);
-
- 'Exit(n)' is actually exit(n). Change the case. You should also have
- stdlib.h included for the prototype for the exit() function.
-
- > }
- > /* We have now opened a file, and are ready to start writing: */
- > bytes_written=Write(file_handle,name,sizeof(name));
- > bytes_written=bytes_written+Write(file_handle,age,sizeof(age));
-
- You cannot get sizeof( name ), as name is an uninitialize pointer. Please
- see my reference to malloc above. As I used malloc, we will not use sizeof,
- as you cannot get a sizeof of a generic pointer...only a struct, union, or
- dynamically allocated array.
-
- BytesWritten = fwrite( name, 80, 1, fpFile ) ;
- BytesWritten = fwrite( &age, sizeof( age ), 1, fpFile ) ;
-
- >
- > if(bytes_written!=sizeof(name)+sizeof(age))
-
- if( BytesWritten != sizeof( age )+80 )
-
- > {
- > printf("Could not save the list!\n");
- > Close(file_handle);
-
- fclose( fpFile ) ;
- free( name ) ;
-
- > Exit(0);
-
- exit( 0 ) ;
-
- > }
- > else
- > printf("Saved successfully!\n");
- > printf("Memory cleared!\n");
- > name="";
-
- strcpy( name, "" ) ;
-
- > age=0;
- > printf("Loading!\n");
- > Seek(file_handle,0,OFFSET_BEGINNING);
-
- fseek( fpFile, 0L, SEEK_SET ) ;
-
- > bytes_read=Read(file_handle,name,sizeof(name));
- > bytes_read=bytes_read+Read(file_handle,age,sizeof(age));
-
- BytesRead = fread( name, 80, 1, fpFile ) ;
- BytesRead = fread( &age, sizeof( age ), 1, fpFile ) ;
-
- > if(bytes_written!=sizeof(name)+sizeof(age))
-
- Shouldn't the above be 'bytes_read'?
-
- > {
- > printf("Could not read the list!\n");
- > Close(file_handle);
- > Exit(0);
-
- fclose( fpFile ) ;
- free( name ) ;
- exit( 0 ) ;
-
- > }
- > /* Print out the data: */
- > printf("Hello, %s!\",name);
- > printf("You are %d years old!\n",age);
- > /* Close the file: */
- > Close(file_handle);
-
- fclose( fpFile ) ;
- free( name ) ;
-
- > }
- >
- > Program END:
- >
- > Any help if MUCH appreciated...
- >
- > I am using the SAS/C 6.50 compiler...
- >
- > Also, is there any of you whom might have a routine to figure up random (or
- > even psuedo-random) numbers?? Some algorithm, or such, as I need one BADLY!!
-
- Your compiler should have one. Reference the srand() function, located in
- your stdlib.h header.
-
- BTW, I'm curious. What operating system is the above compiler for? Is it
- for MS-DOS? It doesn't seem to conform to any standards, whatsoever. Or,
- perhaps you could be misinterpreting your manual.
-
- ttfn.
-
- +------------------------------------------------------+
- ! OS/2 - The Champion of Operating Systems !
- ! Enitharmon/2 - A Powerful BBS Package !
- ! What a great pair! danlynes@res.com !
- ! www.res.com/~danlynes/index.html !
- ! www.res.com/~danlynes/Enitharmon!2/index.html !
- ! The Enitharmon Beta project is now under way. !
- ! To subscribe to the mailing list, send e-mail to !
- ! danlynes@res.com or daniel.lynes@ideasnet.com !
- +------------------------------------------------------+
-
-